package com.whitedust.top60;

import java.util.Arrays;

/**
 *
 * @author A
 * @since 2025/11/10 16:36
 */

public class Top53 {
    public int coinChange(int[] coins, int amount) {
        //dp[i]表示该金额的最少硬币数
        int[] dp = new int[amount + 1];
        int max = Integer.MAX_VALUE;
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if (dp[j - coins[i]] != max) {
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }
        return dp[amount] == max ? -1 : dp[amount];
    }
}

